package com.sanron.sunweather.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.RelativeLayout;

/**
 * 花瓣容器
 * @author Administrator
 *
 */
public class PetalContanier extends RelativeLayout{

	private OnItemClickListener onItemClickListener;
	public PetalContanier(Context context, AttributeSet attrs) {
		super(context, attrs);
		
		for(int i=0; i<5; i++){
			PetalView petalView = new PetalView(context);
			LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
			lp.addRule(RelativeLayout.CENTER_HORIZONTAL);
			lp.addRule(RelativeLayout.ALIGN_BOTTOM);
			petalView.setLayoutParams(lp);
			addView(petalView);
			final int position = i;
			petalView.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					if(onItemClickListener!=null){
						onItemClickListener.onItemClick(v,position);
					}
				}
			});
		}
		
	}

	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b) {
		super.onLayout(changed, l, t, r, b);
		//设置倾斜度
		int width = getChildAt(0).getMeasuredWidth();
		int height = getChildAt(0).getMeasuredHeight();
		for(int i=0; i<5; i++){
			if(i==2) continue;
			View child = getChildAt(i);
			child.setPivotX(width/2);
			child.setPivotY(height);
			child.setRotation(-72+i*36);
		}
	}
	
	
	public OnItemClickListener getOnItemClickListener() {
		return onItemClickListener;
	}

	public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
		this.onItemClickListener = onItemClickListener;
	}

	/**
	 * 花瓣点击监听
	 * @author Administrator
	 *
	 */
	public static interface OnItemClickListener{
		public void onItemClick(View v,int position);
	}
}
